/**
 * Displays dialog windows ala MacOSX
 */

if (!Window)
	var Window = {};

Window.Base = new Class(
{
	initialize: function(options)
	{
		// options
		this.setOptions({
			'className':  'window',
			'id':         'window' + new Date().getTime(),
			'title':      null,
			'overlay':    false,
			'btValidate': true,
			'btCancel':   false,
			'onCancel':   Class.empty,
			'onClose':    Class.empty,
			'onValidate': Class.empty
		}, options);
		
		// the window
		this.create();
	},
	
	create: function()
	{
		this.window = new DOM('div', {'id': this.options.id, 'class': this.options.className});
		
		// titlebar
		if (this.options.title)
		{
			this.window.appendChild(new DOM('div', {'class': 'w_titlebar'},
				new DOM('div',
					new DOM('span', {'class': 'w_titletext'}, this.options.title),
					new DOM('a', {'href': '#', 'class': 'w_close'}, 'close').addEvent('click', this.onClose.bindWithEvent(this))
				)
			));
		}
		
		// content
		this.content = new DOM('div', {'class': 'w_content'});
		this.window.appendChild(this.content);
		
		// footer, with buttons
		if (this.options.btCancel || this.options.btValidate)
		{
			var footer  = new DOM('p', {'class': 'w_buttons'});
			if (this.options.btCancel)
				footer.appendChild(new DOM('button', {'class': 'cancel'}, 'Cancel').addEvent('click', this.onCancel.bindWithEvent(this)));
			if (this.options.btValidate)
				footer.appendChild(new DOM('button', {'class': 'ok'}, 'OK').addEvent('click', this.onValidate.bindWithEvent(this)));
			this.window.appendChild(footer);
		}
		
		// semi-transparent background overlay
		this.overlay = new DOM('div', {'id': 'w_overlay'});
	},
	
	position: Class.empty,
	
	// content
	
	setContent: function(content)
	{
		switch($type(content))
		{
			case 'string':
				this.content.innerHTML = content;
				break;
			case 'element':
				this.content.innerHTML = '';
				this.content.appendChild(content);
		}
		return this;
	},
	
	loadContent: function(url, method)
	{
		this.content.appendChild(new DOM('p', {'class': 'loading'}, 'loading...'));
		new Ajax(url, {
			'update' : this.content,
			'method': method || 'get'
		}).request();
		return this;
	},
	
	//display
	
	show: function()
	{
		if (this.options.overlay)
		{
			document.body.appendChild(this.overlay);
			this.overlay.show();
		}
		
		document.body.appendChild(this.window);
		this.position();
		this.window.show();
		
		return this;
	},
	
	hide: function()
	{
		this.window.hide();
		if (this.options.overlay)
			this.overlay.hide();
		return this;
	},
	
	destroy: function()
	{
		this.window.remove();
		this.window = null;
		if (this.options.overlay)
		{
			this.overlay.remove();
			this.overlay = null;
		}
	},
	
	// callbacks
	
	onCancel: function(evt)
	{
		this.options.onCancel();
		this.destroy();
	},
	
	onClose: function(evt)
	{
		this.options.onClose();
		this.destroy();
	},
	
	onValidate: function(evt)
	{
		this.options.onValidate();
		this.destroy();
	}
});
Window.Base.implement(new Options);

Window.Modal = Window.Base.extend(
{
	initialize: function(options)
	{
		options = Object.extend({'overlay': true}, options);
		this.parent(options);
		this.window.addClass('windowModal');
	}
});

Window.Sticky = Window.Base.extend(
{
	initialize: function(element, options)
	{
		this.element = $(element);
		options = Object.extend({'overlay': false, 'btValidate': false}, options);
		this.parent(options);
		this.create();
	},
	
	position: function()
	{
		var pos = this.element.getCoordinates();
		this.window.setStyles({
			'position': 'absolute',
			'left': pos.left + 'px',
			'top':  pos.top + pos.height + 'px'
		});
	}
});
/*
Calendar = Window.Sticky.extend(
{
	initialize: function(input, options)
	{
		this.parent(input, options);
		this.element.addEvent('focus', this.show.bindWithEvent(this));
		this.element.addEvent('blur',  this.hide.bindWithEvent(this));
	}
});
*/